/*
 * Copyright 2020-2022 The OSHI Project Contributors
 * SPDX-License-Identifier: MIT
 */
package oshi.driver.windows.wmi;

import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiQuery;
import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult;
import oshi.annotation.concurrent.ThreadSafe;
import oshi.util.platform.windows.WmiQueryHandler;
import oshi.util.platform.windows.WmiUtil;

/**
 * Utility to query Open Hardware Monitor WMI data for Sensors
 */
@ThreadSafe
public final class LhmSensor {

    private static final String SENSOR = "Sensor";

    /**
     * Sensor value property
     */
    public enum ValueProperty {
        VALUE;
    }

    public enum SensorProperty {
        IDENTIFIER,
        NAME,
        PARENT,
        SensorType,
        VALUE,
        MIN,
        MAX,

        ;
    }

    private LhmSensor() {
    }

    /**
     * Queries the sensor value of an hardware identifier and sensor type.
     *
     * @param h          An instantiated {@link WmiQueryHandler}. User should have already initialized COM.
     * @param identifier The identifier whose value to query.
     * @param sensorType The type of sensor to query.
     * @return The sensor value.
     */
    public static WmiResult<ValueProperty> querySensorValue(WmiQueryHandler h, String identifier, String sensorType) {
        StringBuilder sb = new StringBuilder(SENSOR);
        sb.append(" WHERE Parent = \"").append(identifier);
        sb.append("\" AND SensorType=\"").append(sensorType).append('\"');
        WmiQuery<LhmSensor.ValueProperty> lhmSensorQuery = new WmiQuery<>(WmiUtil.LHM_NAMESPACE, sb.toString(),
            LhmSensor.ValueProperty.class);
        return h.queryWMI(lhmSensorQuery, false);
    }

    public static WmiResult<SensorProperty> querySensorInfo(WmiQueryHandler h, String identifier, String sensorType) {
        StringBuilder sb = new StringBuilder(SENSOR);
        sb.append(" WHERE Parent = \"").append(identifier);
        sb.append("\" AND SensorType=\"").append(sensorType).append('\"');
        WmiQuery<SensorProperty> lhmSensorQuery = new WmiQuery<>(WmiUtil.LHM_NAMESPACE, sb.toString(),
            SensorProperty.class);
        return h.queryWMI(lhmSensorQuery, false);
    }
}
